/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Funciones para manejar las fotogalerias
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var gnPhotogalleryStartX = gnPhotogalleryLeft = gnPagina=0;
var gcPhotoGallery = ''; var gbBlock = false; var gnIntervalId= 0;

function photogalleryMouseoutEvent( e , oThis, cIdPhotoGallery)
{
	if (!e) var e = window.event;
	if ( gcPhotoGallery != cIdPhotoGallery )  photogalleryInit(cIdPhotoGallery);
	if ( ( e.clientX <= gnPhotogalleryPosX || e.clientX > (gnPhotogalleryWidth + gnPhotogalleryPosX)) || ( e.sreenY <= gnPhotogalleryPosY || e.clientY > (gnPhotogalleryHeight + gnPhotogalleryPosY)) )
	{
		gnPhotogalleryStartX = 0;
		clearInterval( gnIntervalId );
	}
}
function photogalleryMoving()
{
	nOffset = ( gnPhotogalleryWidth / 2) -  gnPhotogalleryStartX + gnPhotogalleryPosX;
	if ( ( nOffset > -40 && nOffset < 40 )  || gnPhotogalleryStartX == 0 ) 
	{
		gnPhotogalleryStartX = 0 ;
		clearInterval( gnIntervalId );
	}else
	{
		bReturn = photogalleryMove(gcPhotoGallery, nOffset * 0.1 );
		if ( !bReturn ) 
		{
			gnPhotogalleryStartX = 0 ;
			clearInterval( gnIntervalId );
		}
	}
}
function photogalleryMousemoveEvent(e, oThis, cIdPhotoGallery)
{
	if (!e) var e = window.event;
	if ( gcPhotoGallery != cIdPhotoGallery )  photogalleryInit(cIdPhotoGallery);
	if ( gnPhotogalleryStartX == 0 ) gnIntervalId = setInterval( 'photogalleryMoving(); ', 200 );
	gnPhotogalleryStartX = e.clientX ;
}
function photogalleryInit(cIdPhotoGallery)
{
	obj = oPhotogallery = id( cIdPhotoGallery );
	if ( obj )
	{
		gnPhotogalleryPosY = gnPhotogalleryPosX = 0 ;
		gnPhotogalleryWidth = oPhotogallery.parentNode.offsetWidth;
		gnPhotogalleryHeight = oPhotogallery.parentNode.offsetHeight;
		gnPhotogalleryFondo = oPhotogallery.parentNode.scrollWidth ;
		gnPagina=0;
		while (  obj = obj.offsetParent  )
		{
			gnPhotogalleryPosX += obj.offsetLeft;
			gnPhotogalleryPosY += obj.offsetTop;
		}
		gcPhotoGallery = cIdPhotoGallery;
	}
}
function photogalleryMove(cIdPhotoGallery, nOffset )
{
	var bReturn = false;
	if ( gcPhotoGallery != cIdPhotoGallery )  photogalleryInit(cIdPhotoGallery);
	oContactos = id(cIdPhotoGallery);
	if ( oContactos )
	{
		nLeft = gnPhotogalleryLeft + nOffset;
		nMaxLeft = gnPhotogalleryWidth - gnPhotogalleryFondo;
		
		// Redondea si esta cerca de los extremos
		if ( nLeft > 0 && nOffset > 0 &&  gnPhotogalleryLeft < 0 ) nLeft = 0;
		if ( nLeft < nMaxLeft && nOffset < 0 ) nLeft = nMaxLeft;
		if ( nLeft  <= 0 && nLeft  >= nMaxLeft  )
		{
			gnPhotogalleryLeft  = nLeft;
			oContactos.style.left = gnPhotogalleryLeft + "px";
			bReturn = true;
		} 
		// Si tiene una url de recarga y esta cerca del final busca nuevos valores
		cUrl = oContactos.getAttribute( "url" );
		if ( cUrl && gbBlock == false && gnPagina >= 0 && (gnPhotogalleryFondo + nLeft)  <  gnPhotogalleryWidth * 1.3 ) 
		{
			gbBlock = true;
			send( cUrl + '&nPagina=' + (++gnPagina), refreshPhotogallery, cIdPhotoGallery , false);
			gbBlock = false;
			oPhotogallery = id( cIdPhotoGallery );
			gnPhotogalleryFondo = oPhotogallery.parentNode.scrollWidth ;
		}
	}
	return bReturn;
}
function refreshPhotogallery()
{
	if (goXMLHttpRequest.readyState==4)
	{
		setSending ( false );
		cHtml = goXMLHttpRequest.responseText;
		if ( cHtml  )
		{
			oElement = id( gcElement + '_interior' );
			if ( oElement )
			{
				oElement.innerHTML = oElement.innerHTML  + cHtml;
			}
		} else
		{
			gnPagina = -1;
		}
	}
}
function photogalleryView( cIdPreview, foto, bBackgroundImage  )
{
	oImg = id(cIdPreview);
	if ( bBackgroundImage )
		oImg.style.backgroundImage = 'url(' + foto + ')';
	else
		oImg.src = foto;
	gnPhotogalleryStartX = 0;
	clearInterval( gnIntervalId );
	return false;
}